//
//  GMPlaceHolderView.m
//  gentleman
//
//  Created by 范月成 on 2017/12/6.
//  Copyright © 2017年 fancy. All rights reserved.
//

#import "GMPlaceHolderView.h"
#import "GMUIHelper.h"
#import "VendorHeader.h"

@interface GMPlaceHolderView ()

@property (nonatomic, strong) UIButton *refreshButton;
@property (nonatomic, strong) YYLabel *titleLabel;

@end

@implementation GMPlaceHolderView

- (instancetype)init {
    if (self = [super init]) {
        [self addSubviews];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self addSubviews];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubviews];
    }
    return self;
}

- (instancetype)initWithTitle:(NSString *)title {
    if (self = [super init]) {
        self.title = [title copy];
        [self addSubviews];
    }
    return self;
}

- (void)addSubviews {
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.titleLabel];
    [self addSubview:self.refreshButton];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.equalTo(self).offset(-50);
        make.width.mas_lessThanOrEqualTo(kScreenWidth-40);
    }];
    
    [self.refreshButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(20);
        make.width.mas_lessThanOrEqualTo(kScreenWidth-40);
        make.height.mas_equalTo(44);
    }];
}

#pragma mark - action
- (void)refresh:(UIButton *)sender {
    !self.refreshBlock?:self.refreshBlock();
}

#pragma mark - getter
- (void)setTitle:(NSString *)title {
    _title = title;
    self.titleLabel.text = title;
}

- (YYLabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [GMUIHelper generalTitleLabel];
        _titleLabel.text = self.title;
        _titleLabel.numberOfLines = 0;
    }
    return _titleLabel;
}

- (UIButton *)refreshButton {
    if (!_refreshButton) {
        _refreshButton = [GMUIHelper buttonWithFont:kSystemFont(14)
                                         titleColor:[UIColor themeGray]];
        [_refreshButton setTitle:@"点击刷新" forState:UIControlStateNormal];
        [_refreshButton addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _refreshButton;
}

@end
